import {
	PRINTER_RECORD_KEY,
	PRINTER_INFO_KEY,
	PRINTER_PAPER_SIZE_KEY,
	PRINTER_PAPER_GAP_KEY,
	PRINTER_PAPER_BLINE_KEY,
	connectionModes
} from '@/utils/printer/index.d';
import {
	isArray
} from '@/utils/is';
import {
	openBluetoothAdapter,
	getBluetoothAdapterState,
	startBluetoothSearch,
	stopBluetoothSearch,
	bindBluetoothDevice,
	getBluetoothSevices,
	getBluetoothCharacteristicsByServices,
	releaseBluetoothDevice,
	onBluetoothDeviceState
} from '@/utils/bluetooth/index';
import {
	useI18n
} from "@/locales/setupI18n";

export const state = {
	/** 蓝牙是否已初始 */
	isBluetoothOpened: null,
	/** 蓝牙是否正在搜索中 */
	isBluetoothSearching: null,
}

/** 获取器 */
export const getters = {
	/** 获取蓝牙是否已初始 */
	getBluetoothOpened: (state, getters, dispatch) => {
		return state.isBluetoothOpened === true;
	},
	/** 获取打印机信息 */
	getBluetoothSearching: state => {
		return state.isBluetoothSearching === true;
	},
}

/** 方法 */
export const actions = {
	/** 初始化蓝牙模块 */
	openBluetoothAdapter: async function({
		state,
		commit,
		getters,
		dispatch
	}, mode) {
		const res = await openBluetoothAdapter(mode);
		if (res === true) {
			state.isBluetoothOpened = true;
		} else {
			state.isBluetoothOpened = false;
		}
		return getters.getBluetoothOpened;
	},
	/** 获取本机蓝牙适配器状态 */
	getBluetoothAdapterState: async function({
		state,
		commit,
		getters,
		dispatch
	}, mode) {
		if (getters.getBluetoothOpened !== true) {
			await dispatch('openBluetoothAdapter', mode);
		}
		const res = await getBluetoothAdapterState(mode);
		return res;
	},
	/** 开始蓝牙搜索 */
	startBluetoothSearch: async function({
		state,
		commit,
		getters,
		dispatch
	}, {
		callback,
		mode
	}) {
		if (getters.getBluetoothOpened !== true) {
			await dispatch('openBluetoothAdapter', mode);
		}
		if (getters.getBluetoothOpened === true) {
			if (state.isBluetoothSearching === true) {
				await dispatch('stopBluetoothSearch', mode);
			}
			await startBluetoothSearch((res) => {
				// console.log('startBluetoothSearch_callback', res);
				if (typeof callback === 'function') {
					callback(res)
				}
			}, mode);
			state.isBluetoothSearching = true;
		}
		return getters.getBluetoothSearching;
	},
	/** 停止蓝牙搜索 */
	stopBluetoothSearch: async function({
		state,
		commit,
		getters,
		dispatch
	}, mode) {
		if (getters.getBluetoothOpened !== true) {
			await dispatch('openBluetoothAdapter', mode);
		}
		if (getters.getBluetoothOpened === true) {
			await stopBluetoothSearch(mode);
			state.isBluetoothSearching = false;
		}
		return getters.getBluetoothSearching;
	},
	/** 绑定蓝牙设备 */
	bindBluetoothDevice: async function({
		state,
		commit,
		getters,
		dispatch
	}, {
		deviceId,
		timeout,
		mode
	}) {
		if (getters.getBluetoothOpened !== true) {
			await dispatch('openBluetoothAdapter', mode);
		}
		if (getters.getBluetoothOpened === true) {
			const res = await bindBluetoothDevice(deviceId, timeout, mode);
			return res;
		} else {
			return false;
		}
	},
	/** 获取蓝牙设备的所有服务(service) */
	getBluetoothSevices: async function({
		state,
		commit,
		getters,
		dispatch
	}, {
		deviceId,
		mode
	}) {
		if (getters.getBluetoothOpened !== true) {
			await dispatch('openBluetoothAdapter', mode);
		}
		if (getters.getBluetoothOpened === true) {
			const res = await getBluetoothSevices(deviceId, mode);
			return res;
		} else {
			return null;
		}
	},
	/** 获取蓝牙设备指定服务中所有特征值(characteristic) */
	getBluetoothCharacteristics: async function({
		state,
		commit,
		getters,
		dispatch
	}, {
		deviceId,
		serviceId,
		mode
	}) {
		if (getters.getBluetoothOpened !== true) {
			await dispatch('openBluetoothAdapter', mode);
		}
		if (getters.getBluetoothOpened === true) {
			const res = await getBluetoothCharacteristics(deviceId, serviceId, mode);
			return res;
		} else {
			return null;
		}
	},
	/** 获取蓝牙设备的所有服务的所有特征值(characteristic) */
	getBluetoothCharacteristicsByServices: async function({
		state,
		commit,
		getters,
		dispatch
	}, {
		deviceId,
		services,
		mode
	}) {
		if (getters.getBluetoothOpened !== true) {
			await dispatch('openBluetoothAdapter', mode);
		}
		if (getters.getBluetoothOpened === true) {
			const res = await getBluetoothCharacteristicsByServices(deviceId, services, mode);
			return res;
		} else {
			return null;
		}
	},
	/** 释放蓝牙设备 */
	releaseBluetoothDevice: async function({
		state,
		commit,
		getters,
		dispatch
	}, {
		deviceId,
		mode
	}) {
		if (getters.getBluetoothOpened !== true) {
			await dispatch('openBluetoothAdapter', mode);
		}
		if (getters.getBluetoothOpened === true) {
			const res = await releaseBluetoothDevice(deviceId, mode);
			return res;
		} else {
			return false;
		}
	},
}
